(function(){function t(t){this.caller=t.caller;this.formNode=t.formNode;this.controlNode=t.controlNode;this.inputNode=t.inputNode;this.config=t.config}t.prototype={};BX.UserConsent={msg:{title:"MAIN_USER_CONSENT_REQUEST_TITLE",btnAccept:"MAIN_USER_CONSENT_REQUEST_BTN_ACCEPT",btnReject:"MAIN_USER_CONSENT_REQUEST_BTN_REJECT",loading:"MAIN_USER_CONSENT_REQUEST_LOADING",errTextLoad:"MAIN_USER_CONSENT_REQUEST_ERR_TEXT_LOAD"},events:{save:"main-user-consent-request-save",refused:"main-user-consent-request-refused",accepted:"main-user-consent-request-accepted"},current:null,autoSave:false,isFormSubmitted:false,isConsentSaved:false,attributeControl:"data-bx-user-consent",load:function(t){var e=this.find(t)[0];if(!e){return null}this.bind(e);return e},loadAll:function(t,e){this.find(t,e).forEach(this.bind,this)},loadFromForms:function(){var t=document.getElementsByTagName("FORM");t=BX.convert.nodeListToArray(t);t.forEach(this.loadAll,this)},find:function(t){if(!t){return[]}var e=t.querySelectorAll("["+this.attributeControl+"]");e=BX.convert.nodeListToArray(e);return e.map(this.createItem.bind(this,t)).filter((function(t){return!!t}))},bind:function(t){if(t.config.submitEventName){BX.addCustomEvent(t.config.submitEventName,this.onSubmit.bind(this,t))}else if(t.formNode){BX.bind(t.formNode,"submit",this.onSubmit.bind(this,t))}BX.bind(t.controlNode,"click",this.onClick.bind(this,t))},createItem:function(e,n){var i=n.querySelector('input[type="checkbox"]');if(!i){return}try{var s=JSON.parse(n.getAttribute(this.attributeControl));var o={formNode:null,controlNode:n,inputNode:i,config:s};if(e.tagName=="FORM"){o.formNode=e}else{o.formNode=BX.findParent(i,{tagName:"FORM"})}o.caller=this;return new t(o)}catch(t){return null}},onClick:function(t,e){if(t.config.url){return}this.requestForItem(t);e.preventDefault()},onSubmit:function(t,e){this.isFormSubmitted=true;if(this.check(t)){return true}else{if(e){e.preventDefault()}return false}},check:function(t){if(t.inputNode.checked){this.saveConsent(t);return true}this.requestForItem(t);return false},requestForItem:function(t){this.setCurrent(t);this.requestConsent(t.config.id,{sec:t.config.sec,replace:t.config.replace},this.onAccepted,this.onRefused)},setCurrent:function(t){this.current=t;this.autoSave=t.config.autoSave;this.actionRequestUrl=t.config.actionUrl},onAccepted:function(){if(!this.current){return}var t=this.current;this.saveConsent(this.current,(function(){BX.onCustomEvent(t,this.events.accepted,[]);BX.onCustomEvent(this,this.events.accepted,[t]);this.isConsentSaved=true;if(this.isFormSubmitted&&t.formNode&&!t.config.submitEventName){BX.submit(t.formNode)}}));this.current.inputNode.checked=true;this.current=null},onRefused:function(){BX.onCustomEvent(this.current,this.events.refused,[]);BX.onCustomEvent(this,this.events.refused,[this.current]);this.current.inputNode.checked=false;this.current=null;this.isFormSubmitted=false},initPopup:function(){if(this.popup){return}this.popup={}},popup:{isInit:false,caller:null,nodes:{container:null,shadow:null,head:null,loader:null,content:null,textarea:null,buttonAccept:null,buttonReject:null},onAccept:function(){this.hide();BX.onCustomEvent(this,"accept",[])},onReject:function(){this.hide();BX.onCustomEvent(this,"reject",[])},init:function(){if(this.isInit){return true}var t=document.querySelector("div[data-bx-template]");if(!t){return false}var e=document.createElement("DIV");e.innerHTML=t.innerHTML;e=e.children[0];if(!e){return false}document.body.insertBefore(e,document.body.children[0]);this.isInit=true;this.nodes.container=e;this.nodes.shadow=this.nodes.container.querySelector("[data-bx-shadow]");this.nodes.head=this.nodes.container.querySelector("[data-bx-head]");this.nodes.loader=this.nodes.container.querySelector("[data-bx-loader]");this.nodes.content=this.nodes.container.querySelector("[data-bx-content]");this.nodes.textarea=this.nodes.container.querySelector("[data-bx-textarea]");this.nodes.link=this.nodes.container.querySelector("[data-bx-link]");this.nodes.linkA=this.nodes.link?this.nodes.link.querySelector("a"):null;this.nodes.buttonAccept=this.nodes.container.querySelector("[data-bx-btn-accept]");this.nodes.buttonReject=this.nodes.container.querySelector("[data-bx-btn-reject]");this.nodes.buttonAccept.textContent=BX.message(this.caller.msg.btnAccept);this.nodes.buttonReject.textContent=BX.message(this.caller.msg.btnReject);BX.bind(this.nodes.buttonAccept,"click",this.onAccept.bind(this));BX.bind(this.nodes.buttonReject,"click",this.onReject.bind(this));return true},setTitle:function(t){if(!this.nodes.head){return}this.nodes.head.innerHTML=t},setContent:function(t){if(!this.nodes.textarea){return}this.nodes.textarea.innerHTML=t;this.nodes.link.style.display="none";this.nodes.textarea.style.display=""},setUrl:function(t){if(!this.nodes.link){return}this.nodes.linkA.textContent=t;this.nodes.linkA.href=t;this.nodes.link.style.display="";this.nodes.textarea.style.display="none"},show:function(t){if(typeof t=="boolean"){this.nodes.loader.style.display=!t?"":"none";this.nodes.content.style.display=t?"":"none"}this.nodes.container.style.display=""},hide:function(){this.nodes.container.style.display="none"}},cache:{list:[],stringifyKey:function(t){return BX.type.isString(t)?t:JSON.stringify({key:t})},set:function(t,e){var n=this.get(t);if(n){n.data=e}else{this.list.push({key:this.stringifyKey(t),data:e})}},getData:function(t){var e=this.get(t);return e?e.data:null},get:function(t){t=this.stringifyKey(t);var e=this.list.filter((function(e){return e.key==t}));return e.length>0?e[0]:null},has:function(t){return!!this.get(t)}},requestConsent:function(t,e,n,i){e=e||{};e.id=t;var s=this.cache.stringifyKey(e);if(!this.popup.isInit){this.popup.caller=this;if(!this.popup.init()){return}BX.addCustomEvent(this.popup,"accept",n.bind(this));BX.addCustomEvent(this.popup,"reject",i.bind(this))}if(this.current&&this.current.config.text){this.cache.set(s,this.current.config.text)}if(this.current&&this.current.config.url){this.setTextToPopup("",this.current.config.url)}else if(this.cache.has(s)){this.setTextToPopup(this.cache.getData(s))}else{this.popup.setTitle(BX.message(this.msg.loading));this.popup.show(false);BX.ajax.runAction("main.agreement.get",{data:e}).then((t=>{this.cache.set(s,t.data.content.html||"");this.setTextToPopup(this.cache.getData(s))})).catch((()=>{this.popup.hide();alert(BX.message(this.msg.errTextLoad))}))}},setTextToPopup:function(t,e){var n="";var i=t.indexOf("\n");var s=t.indexOf(".");i=i<s?i:s;if(i>=0&&i<=100){n=t.substr(0,i).trim();n=n.split(".").map(Function.prototype.call,String.prototype.trim).filter(String)[0]}this.popup.setTitle(n?n:BX.message(this.msg.title));if(e){this.popup.setUrl(e)}else{this.popup.setContent(t)}this.popup.show(true)},saveConsent:function(t,e){this.setCurrent(t);var n={id:t.config.id,sec:t.config.sec,url:window.location.href};if(t.config.originId){var i=t.config.originId;if(t.formNode&&i.indexOf("%")>=0){var s=t.formNode.querySelectorAll('input[type="text"], input[type="hidden"]');s=BX.convert.nodeListToArray(s);s.forEach((function(t){if(!t.name){return}i=i.replace("%"+t.name+"%",t.value?t.value:"")}))}n.originId=i}if(t.config.originatorId){n.originatorId=t.config.originatorId}BX.onCustomEvent(t,this.events.save,[n]);BX.onCustomEvent(this,this.events.save,[t,n]);if(this.isConsentSaved||!t.config.autoSave){if(e){e.apply(this,[])}}else{this.sendActionRequest("saveConsent",n,e,e)}},sendActionRequest:function(t,e,n,i){n=n||null;i=i||null;e.action=t;e.sessid=BX.bitrix_sessid();e.action=t;BX.ajax({url:this.actionRequestUrl,method:"POST",data:e,timeout:10,dataType:"json",processData:true,onsuccess:BX.proxy((function(t){t=t||{};if(t.error){i.apply(this,[t])}else if(n){n.apply(this,[t])}}),this),onfailure:BX.proxy((function(){var t={error:true,text:""};if(i){i.apply(this,[t])}}),this)})}};BX.ready((function(){BX.UserConsent.loadFromForms()}))})();
//# sourceMappingURL=user_consent.map.js